package com.zhaoyizhe.demo.controller;

import ch.qos.logback.core.util.StringUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/userLogin")
public class UserLoginController {
    @RequestMapping("/login")
    public Boolean login(String userName, String password, HttpSession session){
        if (StringUtil.isNullOrEmpty(userName) || StringUtil.isNullOrEmpty(password)){
            return false;
        }
        if ("zhangSan".equals(userName) && "123456".equals(password)){
            session.setAttribute("userName","zhangSan");
            session.setAttribute("password","123456");
            return true;
        }
        return false;
    }
    @RequestMapping("/getUserName")
    public String getUserName(HttpServletRequest request){
        HttpSession session = request.getSession(true);
        if (session != null){
            String userName = (String) session.getAttribute("userName");
            System.out.println("登录用户：" + userName);
            return userName;
        }
        return "";
    }

}
